package com.bsdk.install.device.electricmeter.v1.cmd;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.bsdk.install.device.constants.LockConstants;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;

@Slf4j
public class ANALYSIS  extends Cmd{

    private JSONObject reData = new JSONObject();


    /***
     * @description
     * @author
     * @throws
     * @return com.alibaba.fastjson2.JSONObject
     * @time 16:51
     */
    public JSONObject dealJsonData(JSONObject jsonObject) {

        int i, j = 0, k;
        String str_data;
        String tmp;
        int len;
        byte[] hex_data = new byte[200];

        if(jsonObject == null){

            reData.put("code", STATUS_ERROR);
            return reData;
        }
        str_data = (String) jsonObject.get("data");
        len = str_data.length();
        if (len == 0) {
            reData.put("code", STATUS_ERROR);
            return reData;
        }
        log.info("str_data：" + str_data);
        len = len / 2;
        //写一个转换函数
        for (i = 0; i < len; i++) {
            k = 2 * i;
            tmp = str_data.substring(k, (k + 2));
            k = Integer.parseInt(tmp, 16);
            hex_data[j++] = (byte) k;

        }
        log.info("16:", Arrays.toString(hex_data));
        log.info("str_data：" + str_data);
        JSONObject retJsonObject;
        //先检测模块的协议
        retJsonObject = Moudle_Packet_analysis(hex_data, j);
        if(retJsonObject.isEmpty()==false)return retJsonObject;
        return Packet_analysis(hex_data, j);

       }

    //fefefefe6863990801000068910833333333343333330716
    public JSONObject Packet_analysis(byte[] m_data, int len) {
        JSONObject jsonObject = new JSONObject();
        int m_rev_state = CMD_RECEIVE_HEAD;
        int data_total_len = 0;
        int rcv_len = 0;
        int j;
        int crc = 0;
        byte rcv_char;
        byte[] m_packet_data = new byte[100];
        byte[] meter_sn_data = new byte[10];
        if (len < 1) return jsonObject;
        j = 0;
        while (len != j) {
            rcv_char = m_data[j++];
            switch (m_rev_state) {
                case CMD_RECEIVE_HEAD:
                    if (rcv_char == 0x68){
                        m_rev_state = CMD_RECEIVE_METER_SN;
                        crc = rcv_char;
                    }
                    break;
                case CMD_RECEIVE_METER_SN://命令包+包类型
                    j = 0;
                    meter_sn_data[j++] = rcv_char;
                    crc += rcv_char;
                    if(j==6)m_rev_state = CMD_RECEIVE_HEAD_2;
                    break;
                case CMD_RECEIVE_HEAD_2:
                    if (rcv_char == 0x68){
                        m_rev_state = CMD_RECEIVE_CMD_DATA;
                        crc += rcv_char;
                    }
                    else m_rev_state = CMD_RECEIVE_HEAD;
                case CMD_RECEIVE_CMD_DATA:
                    rcv_len = 0;
                    m_packet_data[rcv_len++] = rcv_char;
                    crc += rcv_char;
                    if(rcv_len==2)data_total_len = rcv_char;
                    if (rcv_len == data_total_len) m_rev_state = CMD_RECEIVE_CRC;
                    break;
                case CMD_RECEIVE_CRC: //校验和
                    if (rcv_char == crc) {
                        m_rev_state = CMD_RECEIVE_TAIL;
                        //m_packet_data[rcv_len++] = rcv_char;

                    } else
                        m_rev_state = CMD_RECEIVE_HEAD;
                    break;
                case CMD_RECEIVE_TAIL:
                    if (rcv_char == 0x16) {
                        //m_packet_data[rcv_len++] = rcv_char;
                        m_rev_state = CMD_RECEIVE_OK;;
                    } else {
                        m_rev_state = CMD_RECEIVE_HEAD;
                    }
                    break;
                default:
                    break;
            }
            if (CMD_RECEIVE_OK == m_rev_state) {
               // jsonObject = packet_process(m_packet_data, rcv_len);
                Meter_packet_process(meter_sn_data,m_packet_data, rcv_len);
                m_rev_state = CMD_RECEIVE_HEAD;
            }
        }
        return jsonObject;
    }
    /*
        //fefefefe6863990801000068910833333333343333330716
        meter_sn:639908010000
        //len:08
        buf:91083333333334333333
     */
    public JSONObject Meter_packet_process(byte[] meter_sn,byte[] buf, int len) {
        JSONObject jsonObject = new JSONObject();
        int i,j;
        int val,len_data;
        byte m_packet;
        byte m_CMD;
        StringBuilder rcv_data = new StringBuilder();
        for(i = 0;i< len;i++) {
            rcv_data.append(String.format("%02X", buf[i]));
        }
        JSONObject jsonData = new JSONObject();
        StringBuilder tmp_data = new StringBuilder();
        //02010077ECFF
        reData.put("code", STATUS_SUCCESS);
        m_CMD = buf[0];
        len_data = buf[1];
        switch (m_CMD) {
            case METER_TOTOAL_VAL_RESP:
                //buf[4] 保留
                j = 2;
                for(i = 0;i < 6;i++)tmp_data.append(String.format("%02X", meter_sn[5-i]));
                jsonObject.put("MeterSn",tmp_data.toString());
                tmp_data.delete(0, tmp_data.length());
                //val = (buf[j++]-0x33)*0x10000000;
                for(i = 0;i < 8;i++)tmp_data.append(String.format("%c", buf[j++]));
                jsonObject.put("Val",tmp_data.toString());
                //jsonObject = CMD_Respone(buf, len);
                break;
            default:
                break;

        }
        reData.put("data",jsonObject);
        //reData.put("Raw_data",rcv_data);
        return reData;

    }


    public JSONObject Moudle_Packet_analysis(byte[] m_data, int len) {
        JSONObject jsonObject = new JSONObject();
        int m_rev_state = MOUDLE_CMD_RECEIVE_HEAD;
        int data_total_len = 0;
        int rcv_len = 0;
        byte rcv_char;
        byte[] m_packet_data = new byte[100];
        int j = 0, i;
        if (len < 1) return jsonObject;

        while (len != j) {
            rcv_char = m_data[j++];
            switch (m_rev_state) {
                case MOUDLE_CMD_RECEIVE_HEAD:
                    if (rcv_char == 0x02) m_rev_state = MOUDLE_CMD_RECEIVE_LEN1;
                    rcv_len = 0;
                    m_packet_data[rcv_len++] = rcv_char;
                    break;
                case MOUDLE_CMD_RECEIVE_LEN1://
                    m_packet_data[rcv_len++] = rcv_char;
                    data_total_len = rcv_char;
                    m_rev_state = MOUDLE_CMD_RECEIVE_LEN2;
                    break;
                case MOUDLE_CMD_RECEIVE_LEN2://
                    m_packet_data[rcv_len++] = rcv_char;
                    data_total_len = (rcv_char*0x100) + data_total_len + 3;
                    m_rev_state = MOUDLE_CMD_RECEIVE_CMD_DATA;
                    break;
                case MOUDLE_CMD_RECEIVE_CMD_DATA:
                    m_packet_data[rcv_len++] = rcv_char;
                    if (rcv_len == data_total_len) m_rev_state = MOUDLE_CMD_RECEIVE_CRC;
                    break;
                case MOUDLE_CMD_RECEIVE_CRC: //异或
                    if (rcv_char == check_crc(m_packet_data, rcv_len)) {
                        m_rev_state = MOUDLE_CMD_RECEIVE_TAIL;
                        m_packet_data[rcv_len++] = rcv_char;

                    } else{
                        m_packet_data[rcv_len++] = rcv_char;
                        m_rev_state = MOUDLE_CMD_RECEIVE_TAIL;
                    }

                    break;
                case MOUDLE_CMD_RECEIVE_TAIL:
                    if (rcv_char == (byte)0x0FF) {
                        m_packet_data[rcv_len++] = rcv_char;
                        m_rev_state = MOUDLE_CMD_RECEIVE_OK;
                    } else {
                        m_rev_state = MOUDLE_CMD_RECEIVE_HEAD;
                    }
                    break;

                default:
                    break;
            }
            if (MOUDLE_CMD_RECEIVE_OK == m_rev_state) {
                jsonObject = moudl_packet_process(m_packet_data, rcv_len);
                m_rev_state = MOUDLE_CMD_RECEIVE_HEAD;
            }
        }
        return jsonObject;
    }
    public JSONObject moudl_packet_process(byte[] buf, int len) {
        JSONObject jsonObject = new JSONObject();
        int i,j;
        byte m_packet;
        byte m_CMD;
        StringBuilder rcv_data = new StringBuilder();
        for(i = 0;i< len;i++) {
            rcv_data.append(String.format("%02X", buf[i]));
        }
        JSONObject jsonData = new JSONObject();
        StringBuilder tmp_data = new StringBuilder();
        //02010077ECFF
        reData.put("code", STATUS_SUCCESS);
        m_CMD = buf[3];
        switch (m_CMD) {
            case MOUDLE_CMD_REGIST_RESP:
                jsonObject.put("gwChn",buf[4]);
                //jsonObject = CMD_noRespone(buf, len);
                break;
            case MOUDLE_CMD_PING_RESP:
                //buf[4] 保留
                j = 5;
                jsonObject.put("gwChn",buf[j++]);
                for(i = 0;i < 4;i++)tmp_data.append(String.format("%02X", buf[j++]));
                jsonObject.put("gwMac",tmp_data.toString());
                tmp_data.delete(0, tmp_data.length());
                for(i = 0;i < 5;i++)tmp_data.append(String.format("%c", buf[j++]));
                jsonObject.put("version",tmp_data.toString());
                //jsonObject = CMD_Respone(buf, len);
                break;
            case MOUDLE_CMD_SET_REGIST_RESP://Regist 指令
                j = 6;
                //for(i = 0;i < 4;i++)tmp_data.append(String.format("%02X", buf[j++]));
                // jsonObject.put("gwMac",tmp_data.toString());
                break;
            case MOUDLE_CMD_REQ_PING_RESP://查询Regist
                j = 6;
                for(i = 0;i < 4;i++)tmp_data.append(String.format("%02X", buf[j++]));
                jsonObject.put("gwMac",tmp_data.toString());
            default:
                break;

        }
        reData.put("data",jsonObject);
        //reData.put("Raw_data",rcv_data);
        return reData;

    }
  //fefefefe6863990801000068910833333333343333330716




}